<?php

namespace app\admin\controller;

use app\admin\model\VersionModel;
use think\Db;

class Version extends Base
{

    /**
     * [index 版本列表]
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['create_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['create_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['create_time'] = ['between time',[$start,$end]];
            }
            if(isset($key)&&$key!=""){
                $map['oldversion'] = ['like',"%" . $key . "%"];
            }
            $field=input('field');//字段
            $order=input('order');//排序方式
            if($field && $order){
                $od=$field." ".$order;
            }else{
                $od="create_time desc";
            }
            $version = new VersionModel();
            $Nowpage = input('get.page') ? input('get.page'):1;
            $limits = input("limit")?input("limit"):10;
            $count = $version->getAllVersion($map);  //总数据
            $lists = $version->getVersionByWhere($map, $Nowpage, $limits,$od);
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch("version/index");
    }

    /**
     * [add_ver 添加版本]
     * @return mixed|\think\response\Json
     */
    public function add_ver()
    {
        if(request()->isPost()){
            extract(input());
            $param = input('post.');
            $version = new VersionModel();
            $flag = $version->insertVersion($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        return $this->fetch('version/add_ver');
    }

    /**
     * [edit_nav 编辑版本]
     * @return mixed|\think\response\Json
     */
    public function edit_ver()
    {
        $version = new VersionModel();
        if(request()->isPost()){
            $param = input('post.');
            $flag = $version->editVersion($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $version = new VersionModel();
        $data = $version->getOneVersion($id);
        if(!empty($data['img'])){
            $data['img'] = trim($data['img'],',');
        }else{
            $data['img'] = '';
        }
        $this->assign('version',$data);
        return $this->fetch();
    }

    /**
     * [del_nav 删除版本]
     * @return mixed|\think\response\Json
     */
    public function del_ver()
    {
        $id = input('param.id');
        $version = new VersionModel();
        $flag = $version->delVersion($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * [nvigation_state 版本状态]
     * @return \think\response\Json
     */
    public function version_state()
    {
        extract(input());
        $version = new VersionModel();
        $flag = $version->versionState($id,$num);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * [batchDelVersion 批量删除版本]
     * @return \think\response\Json
     */
    public function batchDelVersion(){
        extract(input());
        if(empty($ids)){
            return json(['code'=>100,'msg'=>'请选择要删除的记录！']);
        }
        $version = new VersionModel();
        $flag = $version->batchDelVersion($ids);
        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
    }

    /**
     * [usingVersion 批量启用版本]
     * @return \think\response\Json
     */
    public function usingVersion(){
        extract(input());
        $list = [];
        if($ids){
            $ids = explode(',',$ids);
            for($i=0;$i<count($ids);$i++){
                $param = [
                    'id'=>$ids[$i],
                    'status'=>1
                ];
                $list[] = $param;
            }
        }
        $version = new VersionModel();
        $flag = $version->usingVersion($list);
        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
    }

    /**
     * [forbiddenVersion 批量禁用版本]
     * @return \think\response\Json
     */
    public function forbiddenVersion(){
        extract(input());
        $list = [];
        if($ids){
            $ids = explode(',',$ids);
            for($i=0;$i<count($ids);$i++){
                $param = [
                    'id'=>$ids[$i],
                    'status'=>2
                ];
                $list[] = $param;
            }
        }
        $version = new VersionModel();
        $flag = $version->forbiddenVersion($list);
        return json(['code' => $flag['code'], 'msg' => $flag['msg']]);
    }

    public function printOrder(){
        return $this->fetch('article/order');
    }
}